package com.hotdog.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hotdog.mapper.CommentMapper;
import com.hotdog.pojo.Comment;
import com.hotdog.vo.PageResult;
import com.hotdog.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 评论
 * @author 郭鲁政
 */
@Service
public class CommentServiceImpl implements CommentService{
    @Autowired
    private CommentMapper commentMapper;
    @Override
    public PageResult getInfo(PageResult pageResult) {
        Page<Comment> pageSearchPage = new Page<>(pageResult.getPageNum(), pageResult.getPageSize());
        boolean b = StringUtils.hasLength(pageResult.getQuery());
        QueryWrapper<Comment> title = new QueryWrapper<Comment>().eq("viewid", pageResult.getQuery());
        IPage<Comment> indexInfoPage = commentMapper.selectPage(pageSearchPage, title);
        List<Comment> records = indexInfoPage.getRecords();
        long total=commentMapper.selectCount(null);
        pageResult.setTotal(total).setRows(records);

        return pageResult;
    }

    @Override
    public SysResult changestatus(Comment comment) {
        QueryWrapper<Comment> wrapper = new QueryWrapper<>();


        int i = commentMapper.updateById(comment);
        if (i==1){
            return SysResult.success();
        }
        return SysResult.fail();
    }

    @Override
    public SysResult insertInfo(Comment comment) {
        String name = SecurityContextHolder.getContext().getAuthentication().getName();
        comment.setUsername(name);
        int insert = commentMapper.insert(comment);
        if (insert==1){
            return SysResult.success();
        }
        return SysResult.fail();
    }
}
